package project.ccso.top.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import project.ccso.top.bean.GridPageBean;
import project.ccso.top.bean.ReturnResult;
import project.ccso.top.entity.Province;
import project.ccso.top.enumerate.Result;
import project.ccso.top.service.ProvinceService;
import project.ccso.top.service.impl.ProvinceServiceImpl;

/**
 * ProvinceServlet 处理与Province相关的请求
 * 
 * @author aibinxiao
 * @date 2018年8月23日 下午3:10:22
 */
@WebServlet("/ProvinceServlet")
public class ProvinceServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = LoggerFactory.getLogger(ProvinceServlet.class);
	// 数据json格式转换
	private static ObjectMapper mapper = new ObjectMapper();
	private ProvinceService provinceService = new ProvinceServiceImpl();

	/**
	 * 条件分页查询省份信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void findByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		String pageNo = request.getParameter("page"); // pageNum 页码
		String pageSize = request.getParameter("rows"); // pageSize 每页条数
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		GridPageBean<Province> pageBean = null;
		try {
			pageBean = provinceService.findByPage(pageNo, pageSize, id, name, startDate, endDate);
			result.setResult(Result.SUCCESS.name());
			result.setMessage("查询第" + pageNo + "页的省份信息成功！");
		} catch (Exception e) {
			result.setResult(Result.FAILED.name());
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		result.setData(pageBean);
		writer.print(mapper.writeValueAsString(result));
	}

	/**
	 * 根据id查找省份
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void findById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		String id = request.getParameter("id");
		Province province = null;
		try {
			province = provinceService.findById(id);
			result.setResult(Result.SUCCESS.name());
			result.setMessage("查询Id为" + id + "的省份信息成功！");
			logger.info(result.getMessage());
		} catch (Exception e) {
			result.setResult(Result.FAILED.name());
			result.setMessage(e.getMessage());
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		result.setData(province);
		writer.print(mapper.writeValueAsString(result));
	}

	/**
	 * 根据id修改省份的信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void saveOrUpdateById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		String id = request.getParameter("id");
		String provinceName = request.getParameter("name");
		boolean flag = false;
		try {
			flag = provinceService.saveOrUpdateById(id, provinceName);
			if (flag) {
				result.setResult(Result.SUCCESS.name());
				if(StringUtils.isEmpty(id)) {
					result.setMessage("添加名称为" + provinceName + "的省份信息成功！");
				}else {
					result.setMessage("修改Id为" + id + "的省份信息成功！");
				}
				logger.info(result.getMessage());
			} 
		} catch (Exception e) {
			result.setResult(Result.FAILED.name());
			result.setMessage(e.getMessage());
			logger.error(e.getMessage());
		}
		result.setData(flag);
		writer.print(mapper.writeValueAsString(result));
	}
	
	/**
	 * 查找所有省份的名字和id*
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selectAllProvince(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		List<Province> list = null;
		try {
			list = provinceService.selectAllProvince();
			result.setResult(Result.SUCCESS.name());
			result.setMessage("查询所有省份信息成功！");
		} catch (Exception e) {
			result.setResult(Result.FAILED.name());
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		result.setData(list);
		writer.print(mapper.writeValueAsString(result));
		
	}
	
	/**
     * 省份信息屏蔽或启用
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void updateShieldStatus(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		String id = request.getParameter("id");
		String statusStr = request.getParameter("status");
		boolean flag = false;
		try {
			flag = provinceService.updateShieldStatus(id, statusStr);
			result.setResult(Result.SUCCESS.name());
			if("0".equals(statusStr)) {
				result.setMessage("id为" + id + "的省份启用成功！");
			}else if("1".equals(statusStr)) {
				result.setMessage("id为" + id + "的省份屏蔽成功！");
			}
			logger.info(result.getMessage());
		} catch (Exception e) {
			result.setResult(Result.FAILED.name());
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		result.setData(flag);
		writer.print(mapper.writeValueAsString(result));
	}
	
}
